<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar
    class="page-nav-bar" fixed>
    <van-button
    class="search-btn"
    slot="title"
    type="info"
    size="small"
    icon="search"
    round
    to="search">
    搜索
    </van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- /teb 标签页 -->
    <!-- v-model="active"是表示默认显示的标签内容 -->
    <van-tabs
    class="channel-tabs"
    v-model="active" animated swipeable>
       <van-tab v-for="channel in channels" :key="channel.id" :title="channel.name">
         <!-- 文章列表 -->
        <article-list :channel = "channel"/>
       </van-tab>
       <div slot="nav-right" class="placeholder">
       </div>
       <div
       slot="nav-right"
       class="hamburger-btn"
       @click="isChennelEditShow=true">
         <i class="iconfont-heima icon-heimagengduo"></i>
       </div>
    </van-tabs>
    <!-- /teb 标签页 -->
    <!-- 频道编辑弹出层 -->
    <van-popup
         v-model="isChennelEditShow"
         closeable
         close-icon-position="top-left"
         position="bottom"
         :style="{ height: '100%' }">
         <channel-edit
         :my-channels="channels"
         :active="active"
         @update-active="onUpdateActive"/>
    </van-popup>
  </div>
</template>

<script>
import ArticleList from './components/article-list'
import { getUserChannels } from '@/api/user'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels () {
      try {
        // 进行判断看是否登录
        let channels = []
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) {
          // 登录或者本地没有数据直接获取后端默认数据
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录且本地有数据则直接获取本地数据使用
          channels = localChannels
        }
        // 将数据更新到组件中
        this.channels = channels
      } catch (err) {
        // console.log('获取信息失败请稍后重试')
        this.$toast('获取频道列表数据失败')
      }
    },
    onUpdateActive (index, isChennelEditShow = true) {
      // console.log('home', index)
      this.active = index
      // 数据改变时关闭频道弹层
      this.isChennelEditShow = isChennelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  /deep/.van-nav-bar__title {
      max-width: unset;
    }
  .search-btn {
      height: 64px;
      width: 555px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      width: 100%;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background: #3296fa;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      i.icon-heimagengduo {
        font-size: 33px;
      }
      &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 2px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
      }
    }
  }
}
</style>
